package com.qf.sy2103.springboot.Controller.User;


import cn.hutool.db.meta.Table;

import com.qf.sy2103.springboot.Service.ClientService;
import com.qf.sy2103.springboot.Util.Constant;
import com.qf.sy2103.springboot.entity.Client;
import com.qf.sy2103.springboot.vo.Clientvo;
import com.qf.sy2103.springboot.vo.Tablevo;
import com.qf.sy2103.springboot.vo.pagevo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Objects;

@Slf4j
@Controller
@RequestMapping("/client")
public class ClientController {

    @Autowired
    private ClientService clientService;

    //分页展示数据
    @ResponseBody
    @RequestMapping("/getAll")
    public Tablevo<Client> getAll(pagevo pagevo, Client client){
        log.debug("入参是 {} {}",pagevo,client);
        Tablevo<Client> tablevo = clientService.getAll(pagevo,client);
        log.debug("返回的参数是 {}",tablevo);
        return tablevo;
    }

    //增加客户数据
    @ResponseBody
    @RequestMapping("/add")
    public Clientvo addClient(Client client){
        log.debug("入参为 {}",client);
        Clientvo clientvo = new Clientvo();
        Client client1 = clientService.selectCilent(client.getUsername());
        if (Objects.nonNull(client1)){
            clientvo.setMessage(Constant.CLIENT_ADD_FILL);
            return clientvo;
        }
        Boolean aBoolean =  clientService.addClient(client);
        log.debug("增加数据是否成功 {}",aBoolean);
        if (aBoolean){
            clientvo.setStatus(Constant.CLIENT_ADD_SUCCESS);
            return clientvo;
        }
        clientvo.setMessage(Constant.CLIENT_ADD_FAL);
        return clientvo;
    }

    //删除客户数据
    @ResponseBody
    @RequestMapping("/delete")
    public Clientvo deleteClient(@RequestParam(value = "ids" ,required = false) List<Integer> ids){
        log.debug("入参为 {}",ids);
        Boolean a = clientService.deleteClient(ids);
        Clientvo clientvo = new Clientvo();
        if (a){
            clientvo.setStatus(Constant.CLIENT_DELETE_SUCCESS);
            log.debug("删除成功");
            return clientvo;
        }
        clientvo.setMessage(Constant.CLIENT_DELETE_FAL);
        return clientvo;
    }


    //修改客户数据
    @ResponseBody
    @RequestMapping("/update")
    public Clientvo update(Client client){
        log.debug("入参为 {}",client);
        Clientvo clientvo = new Clientvo();
        //名字不可重复
        Client client1 = clientService.selectCilent(client.getUsername());
        if (Objects.nonNull(client1)){
            clientvo.setMessage(Constant.CLIENT_ADD_FILL+"请修改用户名称或清空用户名");
            return clientvo;
        }
        Boolean a = clientService.updateClient(client);

        if (a){
            clientvo.setStatus(Constant.CLIENT_UPDATE_SUCCESS);
            return clientvo;
        }
        clientvo.setMessage(Constant.CLIENT_UPDATE_FALE);
        return clientvo;
    }
}
